<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\RestoreMySQL;
use app\util\Mysql;
$database = Mysql::init();

$backup_directory = APP_PATH.'/backup/';
$backup_file = $_POST['backup_file'];
$backup_path = $backup_directory . $backup_file;
$config = DATABASE_CONFIG['mysql'];
$restore = new RestoreMySQL($config['hostname'],$config['username'],$config['password'],$config['database'], $backup_path);
try {
    $restore->restore();
    $username = $_SESSION['username'];
    $client_ip = getClientIP();
    $timestamp = time();
    $database->insert("secure_info",["user"=>$username,"ip"=>$client_ip,"times"=>$timestamp,"content"=>"还原了数据库。"]);

    $sqlinfo = array("success" => "ture", "msg" => "数据库已还原");
    echo json_encode($sqlinfo);
} catch (\Exception $e) {
    echo '还原失败：' . $e->getMessage();
}
